package com.zbkj.admin.scheduled;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.mybatisflex.core.query.QueryWrapper;
import com.zbkj.admin.model.order.Order;
import com.zbkj.admin.model.order.OrderDetail;
import com.zbkj.admin.model.product.Product;
import com.zbkj.admin.pojo.EbDividendRecordEntity;
import com.zbkj.admin.pojo.EbShopFlowingWaterEntity;
import com.zbkj.admin.service.*;
import com.zbkj.admin.third_party.BracketSetApi;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

/**
 * 订单定时任务
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class OrderScheduled {
    @Autowired
    private OrderService orderService;
    @Autowired
    private BracketSetApi bracketSetApi;

    /**
     * 根据平台订单号、获取微信流水号
     */
    @Scheduled(cron = "0 */10 * * * ? ")
    @Transactional(rollbackFor = Exception.class)
    public void sysOrderOutTradeNoByPt() {
        // 获取所有订单中 微信订单号 为PT****的数据
        List<Order> orderList = orderService.findOrderListByPt();
        // 循环所有订单数据
        orderList.forEach(order -> {
            try {
                // 查询支付结果
                CompletableFuture<JSONObject> json = bracketSetApi.query(order.getOutTradeNo());
                JSONObject payJson  = json.get();
                // 根据支付状态 返回微信支付流水 (00:支付成功)
                String bankCode = payJson.getByPath("data.bank_code", String.class);
                if("00".equals(bankCode)){
                    JSONObject wxJson = new JSONObject(payJson.getByPath("data.wx_response", String.class));
                    // 获取所需参数
                    Map<String, String> map = MapUtil.newHashMap();
                    map.put("transaction_id", payJson.getByPath("data.out_trans_id", String.class));
                    map.put("openid", wxJson.get("openid").toString());
                    map.put("hfSeqId", payJson.getByPath("data.org_hf_seq_id", String.class));
                    map.put("reqSeqId", payJson.getByPath("data.org_req_seq_id", String.class));
                    map.put("merchant_Id", payJson.getByPath("data.fee_huifu_id", String.class));
                    // 设置修改参数
                    order.setOutTradeNo(payJson.getByPath("data.out_trans_id", String.class));
                    order.setExpand(JSON.toJSONString(map));
                    // 修改数据
                    orderService.updateById(order);
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

}
